﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.用户控件
{
    public partial class GetString : UserControl
    {
        public GetString()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string yuan = textBox1.Text;
            string huan = textBox2.Text;

            textBox4.Text = textBox3.Text.Replace(yuan, huan);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string zhi1 = textBox3.Text;
            string err = "";
            string jieguo = "";

            string[] list = zhi1.Split(Environment.NewLine.ToCharArray());
            int i = 1;
            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    if (checkBox1.Checked)
                    {
                        if (GetGSHString2(item, ref err))
                        {
                            jieguo += err + "\r\n";
                        }
                        else
                        {
                            MessageBox.Show("第" + i + "条，" + err);
                            return;
                        }
                    }
                    else
                    {
                        if (GetGSHString(item, ref err))
                        {
                            jieguo += err + "\r\n";
                        }
                        else
                        {
                            MessageBox.Show("第" + i + "条，" + err);
                            return;
                        }
                    }

                    i++;
                }
            }

            textBox4.Text = jieguo;

        }

        public bool GetGSHString(string zhi1, ref string err)
        {

            int shu1Str = int.Parse(shu1.Text);
            int shu2Str = int.Parse(shu2.Text);
            string zi1Str = zi1.Text;
            string zi2Str = zi2.Text;

            int kaishi = 0;
            int jieshu = 0;

            if (!string.IsNullOrEmpty(zi1Str) && shu1Str != 0)
            {
                string linshi = zhi1;
                if (shu1Str == 1)
                {
                    kaishi = linshi.IndexOf(zi1Str);
                }
                else
                {
                    for (int i = 1; i < shu1Str; i++)
                    {
                        linshi = linshi.Substring(linshi.IndexOf(zi1Str) + zi1Str.Length);
                        kaishi = linshi.IndexOf(zi1Str);
                    }
                    kaishi = (zhi1.Length - linshi.Length) + kaishi;
                }
                if (kaishi == -1)
                {
                    err = "找不到第" + shu1Str + "个【" + zi1Str + "】字符";
                    return false;
                }
                kaishi += (zi1Str.Length);
            }

            if (!string.IsNullOrEmpty(zi2Str) && shu2Str != 0)
            {
                string linshi = zhi1;
                if (shu2Str == 1)
                {
                    jieshu = linshi.LastIndexOf(zi2Str);
                }
                else
                {
                    for (int i = 1; i < shu2Str; i++)
                    {
                        linshi = linshi.Substring(0, linshi.LastIndexOf(zi2Str));
                        jieshu = linshi.LastIndexOf(zi2Str);
                    }
                }
            }

            if (jieshu == -1)
            {
                err = "找不到第" + shu2Str + "个【" + zi2Str + "】字符";
                return false;
            }
            else if (jieshu == 0) { jieshu = zhi1.Length; }

            if ((jieshu - kaishi) < 0)
            {
                err = "结束位置不能小于开始位置";
                return false;
            }

            err = zhi1.Substring(kaishi, jieshu - kaishi);
            return true;

        }

        public bool GetGSHString2(string zhi1, ref string err)
        {

            int shu1Str = int.Parse(shu1.Text);
            int shu2Str = int.Parse(shu2.Text);
            string zi1Str = zi1.Text;
            string zi2Str = zi2.Text;

            int kaishi = 0;
            int jieshu = 0;

            if (!string.IsNullOrEmpty(zi1Str) && shu1Str != 0)
            {
                string linshi = zhi1;
                if (shu1Str == 1)
                {
                    kaishi = linshi.IndexOf(zi1Str);
                }
                else
                {
                    for (int i = 1; i < shu1Str; i++)
                    {
                        linshi = linshi.Substring(linshi.IndexOf(zi1Str) + zi1Str.Length);
                        kaishi = linshi.IndexOf(zi1Str);
                    }
                    kaishi = (zhi1.Length - linshi.Length) + kaishi;
                }
                kaishi += (zi1Str.Length);
                if (kaishi == -1)
                {
                    //err = "找不到第" + shu1Str + "个【" + zi1Str + "】字符";
                    //return false;
                    kaishi = 0;
                }
            }

            if (!string.IsNullOrEmpty(zi2Str) && shu2Str != 0)
            {
                string linshi = zhi1;
                if (shu2Str == 1)
                {
                    jieshu = linshi.LastIndexOf(zi2Str);
                }
                else
                {
                    for (int i = 1; i < shu2Str; i++)
                    {
                        linshi = linshi.Substring(0, linshi.LastIndexOf(zi2Str));
                        jieshu = linshi.LastIndexOf(zi2Str);
                    }
                }
            }

            if (jieshu == -1)
            {
                //err = "找不到第" + shu2Str + "个【" + zi2Str + "】字符";
                //return false;
                jieshu = zhi1.Length;
            }
            else if (jieshu == 0) { jieshu = zhi1.Length; }

            if ((jieshu - kaishi) < 0)
            {
                err = "结束位置不能小于开始位置";
                return false;
            }

            err = zhi1.Substring(kaishi, jieshu - kaishi);
            return true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string tou = textBox5.Text;
            string wei = textBox6.Text;
            textBox4.Text = "";

            string zhi1 = textBox3.Text;
            
            string[] list = zhi1.Split(Environment.NewLine.ToCharArray());
            
            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    textBox4.Text += tou + item + wei + "\r\n";
                }
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string zhi1 = textBox3.Text;
            textBox4.Text = "";
            
            string[] list = zhi1.Split(Environment.NewLine.ToCharArray());
            int zhenshi = (list.Count() + 1)/ 2;

            for (int i = 0; i < list.Count(); i++)
            {
                if (i%2 == 0)
                {
                    if (string.IsNullOrEmpty(list[i]))
                    {
                        textBox4.Text += textBox7.Text + "\r\n";
                    }
                    else
                    {
                        textBox4.Text += list[i] + "\r\n";
                    }
                    
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox3.Text = textBox4.Text;
            textBox4.Text = "";
        }

        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Focus();
            textBox3.SelectAll();
        }

        private void textBox4_DoubleClick(object sender, EventArgs e)
        {
            textBox4.Focus();
            textBox4.SelectAll();
        }
    }
}
